package cn.dt70.controller;

import cn.dt70.pojo.AqbPacket;
import cn.dt70.pojo.AqbProject;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.*;
import cn.dt70.utils.PageSupport;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

@RequestMapping("/backend/")
public class BackendController {
    @Resource
    private AqbUserService userService;//注入用户servic层
    @Resource
    private AqbProjectService aqbProjectService;//注入项目service层
    @Resource
    private AqbProductService aqbProductService;//注入项目service层
    @Resource
    private AqbPacketService aqbPacketService;



    //    用户信息列表
    @RequestMapping("userInfoList")
    public String userinfoList(@RequestParam(value = "userCode",required = false)String userCode,
                                @RequestParam(value = "userName",required = false)String userName,
                                PageSupport ph, Model model, HttpSession session){
//        if(session.getAttribute(Constrants.BACKEND_USER) == null){
//            return "";
//        }
        List<AqbUser> userList = userService.getAllUserInfo(userCode,userName,ph);
        model.addAttribute("userCode",userCode);
        model.addAttribute("userName",userName);
        model.addAttribute("userInfoList",userList);
        //分页信息
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());
        model.addAttribute("totalCount",ph.getTotalCount());
        model.addAttribute("totalPageCount",ph.getTotalPageCount());
        return "backend/userInfoList";
    }

//    后台欢迎页面
    @RequestMapping("welcome")
    public String welcome(){
        return "backend/main";
    }

//    用户详细信息页面
    @RequestMapping("getUserInfoById")
    public String getUserInfoById(@RequestParam(value = "userCode",required = false) String userCode,
                                    Model model){
        AqbUser aqbUser = userService.getUserInfoById(userCode);
        model.addAttribute("userCode",userCode);
        model.addAttribute("aqbUser",aqbUser);
        return "backend/detailedUserInfo";
  }

//  根据用户编码删除用户
    @RequestMapping(value = "delUserInfo",method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
   public String delUserInfo(@RequestParam(value = "userCode")String userCode){
        if (userService.delUserInfo(userCode)){
            System.out.println("{\"result\":\"删除成功\"}");
            return "{\"result\":\"删除成功\"}";
        }
        System.out.println("{\"result\":\"删除失败\"}");
        return "{\"result\":\"删除失败\"}";
    }
//    查询投资项目列表信息
    @RequestMapping("proList")
    public String proList(@RequestParam(value = "createrId",required = false)Integer createrId,
                           @RequestParam(value ="proType",required = false) Integer proType,
                           @RequestParam(value = "proStatus",required = false) Integer status,
                           @RequestParam(value = "userName",required = false) String userName,
                           PageSupport ps, Model model, HttpSession session){
//        if (currentPageNo==0){
//            currentPageNo=1;
//        }
//        if (null==createrId){
//            createrId = 0;
//        }
        if (null==proType){
            proType = 0;
        }
        if (null==status){
            status = -1;
        }
        List<AqbProject> aqbProjectList = aqbProjectService.proList(createrId,proType,status,null,userName,ps);
        model.addAttribute("createrId",createrId);
        model.addAttribute("proType",proType);
        model.addAttribute("proStatus",status);
        model.addAttribute("proList",aqbProjectList);
        model.addAttribute("userName",userName);
        //分页信息
        model.addAttribute("currentPageNo",ps.getCurrentPageNo());
        model.addAttribute("totalCount",ps.getTotalCount());
        model.addAttribute("totalPageCount",ps.getTotalPageCount());
        return "backend/projectList";
    }

    //项目详细页面
    @RequestMapping("detailedProInfo")
    public String detailedProInfo(@RequestParam(value = "proCode",required = false)String proCode,
                              Model model){
        AqbProject aqbProject=aqbProjectService.aqbPro(proCode);
        model.addAttribute("proCode",proCode);
        model.addAttribute("aqbPro",aqbProject);
        return "backend/detailedProInfo";
    }

//    //审核页面
//    @RequestMapping("proCheck")
//    public String proCheck(@RequestParam(value ="proCode",required = false)String proCode,
//                            Model model){
//        AqbProject aqbProject=aqbProjectService.aqbPro(proCode);
//
//        model.addAttribute("proCode",proCode);
//        model.addAttribute("aqbPro",aqbProject);
//        return "backend/detailedProInfo";
//    }

    @RequestMapping("checksave")
    @ResponseBody
    public String checkSave(@RequestParam("pSummary")String pSummary,
                             @RequestParam("prostatus")int prostatus,
                             @RequestParam("proCode")String proCode){

        if(aqbProjectService.updSatus(prostatus,proCode)){
            if(aqbProductService.InfoUpd(pSummary,proCode)){
                return "{\"result\":\"T\"}";
            }else{
                return "{\"result\":\"F\"}";
            }
        }
//        return "forward:proList";
        return "{\"result\":\"F\"}";
    }
    //后台查看红包信息
    @RequestMapping("packetList")
    public String proList(PageSupport ps, Model model, HttpSession session){
        List<AqbPacket> aqbPackets = aqbPacketService.getPacketInfo(ps);
        model.addAttribute("aqbPacketList",aqbPackets);
        //分页信息
        model.addAttribute("currentPageNo",ps.getCurrentPageNo());
        model.addAttribute("totalCount",ps.getTotalCount());
        model.addAttribute("totalPageCount",ps.getTotalPageCount());
        return "backend/packet";
    }

    //    后台查看加息券信息
    @RequestMapping("jiaxi")
    public String jiaxiList(PageSupport ps, Model model, HttpSession session){
        List<AqbPacket> jiaxiInfo = aqbPacketService.getjiaxiInfo(ps);
        model.addAttribute("jiaxiInfo",jiaxiInfo);
        //分页信息
        model.addAttribute("currentPageNo",ps.getCurrentPageNo());
        model.addAttribute("totalCount",ps.getTotalCount());
        model.addAttribute("totalPageCount",ps.getTotalPageCount());
        return "backend/jiaxi";
    }


}
